#include <algorithm>
#include <cmath>
#include <cstdio>

typedef long long ll;
const int N = 10000005;
ll ans[2] = {-0x3f3f3f3f3f3f3f3f, 0};
int n;
int a[N], b[N];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("sequence.in", "r", stdin);
  freopen("sequence.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  int m, p, lasp = 0, l, r;
  ll mod = (1 << 30) - 1, x, y, z;
  scanf("%lld%lld%lld%d%d%d", &x, &y, &z, &b[1], &b[2], &m);
  for (int i = 3; i <= n; ++i) {
    b[i] =
        (((((x * b[i - 1]) & mod) + ((y * b[i - 2]) & mod)) & mod) + z) & mod;
  }
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &p, &l, &r);
    for (int j = lasp + 1; j <= p; ++j) {
      a[j] = b[j] % (r - l + 1) + l;
    }
    lasp = p;
  }
  int max = 0, min = 0x7f7f7f7f;
  for (int i = 1; i <= n; ++i) {
    if (i > 1 && i < n &&
        ((a[i - 1] > a[i] && a[i] <= a[i + 1]) ||
         (a[i - 1] < a[i] && a[i] >= a[i + 1]))) {
      ll nxt[2] = {-0x3f3f3f3f3f3f3f3f, -0x3f3f3f3f3f3f3f3f};
      nxt[0] =
          std::max(nxt[0], ans[0] + std::max(max, a[p]) - std::min(min, a[p]));
      if (p != i - 1)
        nxt[0] = std::max(nxt[0], ans[1] + max - min);
      else
        nxt[0] = std::max(nxt[0], ans[1]);
      nxt[1] = std::max(nxt[1], ans[0] + std::max(max, std::max(a[p], a[i])) -
                                    std::min(min, std::min(a[p], a[i])));
      nxt[1] =
          std::max(nxt[1], ans[1] + std::max(max, a[i]) - std::min(min, a[i]));
      ans[0] = nxt[0], ans[1] = nxt[1];
      max = 0, min = 0x3f3f3f3f, p = i;
    } else
      max = std::max(max, a[i]), min = std::min(min, a[i]);
  }
  printf("%lld", std::max(ans[0] + std::max(max, a[p]) - std::min(min, a[p]),
                          ans[1] + max - min));
  return 0;
}